package com.system.honour.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.system.honour.entity.SysPermission;

import java.util.List;

/**
* @Description:    树形工具
* @Author:         Joe
* @CreateDate:     2020/3/30 15:30
*/
public class TreeUtils {

    /**
     * 菜单树
     * @param parentId
     * @param permissionList
     * @param array
     */
    public static void setPermissionsTree(Integer parentId, List<SysPermission> permissionList, JSONArray array){
        for (SysPermission per : permissionList){
            if (per.getParentId().equals(parentId)){
                String string = JSONObject.toJSONString(per);
                JSONObject parent = (JSONObject)JSONObject.parse(string);
                array.add(parent);
                if (permissionList.stream().filter(p -> p.getParentId().equals(per.getId())).findAny() != null){
                    JSONArray child = new JSONArray();
                    parent.put("child",child);
                    setPermissionsTree(per.getId(),permissionList,child);
                }
            }
        }
    }
}
